package com.education.mymediarecorder;

import android.content.Intent;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button mBtnRecorder;
    private SurfaceView mSurfaceDisplay;
    private SurfaceHolder mSurfaceHolder;
    private MediaRecorder mediaRecorder;
    private Button mBtnStop;
    private Button mBtnOpenPermission;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtnRecorder = (Button) findViewById(R.id.btn_recorder);
        mBtnStop = (Button) findViewById(R.id.btn_stop);
        mBtnOpenPermission = (Button) findViewById(R.id.btn_permission);
        mBtnOpenPermission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivity(intent);
            }
        });
        mSurfaceDisplay = (SurfaceView) findViewById(R.id.surface_display);
        mSurfaceHolder = mSurfaceDisplay.getHolder();
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        mBtnRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "开始录制", Toast.LENGTH_SHORT).show();
                //创建录制视频的recorder
                mediaRecorder = new MediaRecorder();
                //设置录制音视频的输入源麦克风
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                //设置视频来源为摄像头
                mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                //输出格式为MP4
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                //设置音频的压缩方式为aac
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                //设置视频的压缩方式为264
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                //设置输出的视频文件
                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/aa.mp4");
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaRecorder.setOutputFile(file.getAbsolutePath());
                //设置屏幕宽高
//                mediaRecorder.setVideoSize(1024, 768);
                //视频帧率
//                mediaRecorder.setVideoFrameRate(24);
                //录制时的预览
                mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        mBtnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "停止录制", Toast.LENGTH_SHORT).show();
                mediaRecorder.stop();
                mediaRecorder.reset();
                mediaRecorder.release();
            }
        });
    }
}
